import React, {useState} from 'react';
import {View, Button, Image} from 'react-native';
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';

import {api} from '../../utils/api';

// 为给定 ID 的 user 创建请求
api({
  method: 'get',
  headers: {Token: 'U2FsdGVkX184ys+OO7y3M3FUNKWWTSJcJpiQvO42PJo='},
  // data: data,
  url: 'https://tpdoc.cn/api_2020/tpCli/user/getUserInfo',
})
  .then(axiosGetData => {
    console.log('axiosGetData');
    console.log(axiosGetData);
  })
  .catch(error => {
    console.log('axios-error');
    console.log(error);
  });

const OtherScreen = ({navigation, route, props}) => {
  const [selectedImage, setSelectedImage] = useState(null);

  function goOther1() {
    console.log(navigation.state);
    navigation.navigate('webview', {
      url: 'https://www.baidu.com',
    });
  }

  const handleChooseImage = async () => {
    const options = {
      mediaType: 'photo',
      title: 'Select Image',
    };

    // console.log(launchCamera);

    // 选择图片
    const result = await launchImageLibrary(options);
    console.log('result');
    console.log(result);
    const source = result.assets[0].uri;
    // console.log(source);
    setSelectedImage(source);

    // // 上传图片
    // const data = new FormData();
    // // 将图片文件添加到FormData
    // data.append('image', {
    //   uri: source,
    //   name: 'image.png',
    //   type: 'image/png',
    // });
    // const option = {
    //   method: 'POST',
    //   headers: {'content-type': 'application/form-data'},
    //   data: data,
    //   url: '图片服务器地址',
    // };
    // const dataResult = await api(option);

    // console.log(123);
    // console.log(dataResult);

    // 调用相机
    // launchCamera(options, response => {
    //   console.log('Response = ', response);

    //   if (response.didCancel) {
    //     console.log('User cancelled image picker');
    //   } else if (response.error) {
    //     console.log('ImagePicker Error: ', response.error);
    //   } else if (response.customButton) {
    //     console.log('User tapped custom button: ', response.customButton);
    //   } else {
    //     const source = {uri: response.uri};
    //     setSelectedImage(source);
    //   }
    // });
  };

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      <Button title="Go to 其他1" onPress={goOther1} />
      <Image source={{uri: selectedImage}} style={{width: 200, height: 200}} />
      {/* <Image
        source={{uri: 'https://tpdoc.cn/api_2020_public/img/saveImg/pr2ciiwwov1636905416903.png'}}
        style={{width: 200, height: 200}}
      />
      <Image
        source={require('./images/img01.png')}
        style={{width: 200, height: 200}}
      /> */}
      <Button title="选择图片" onPress={handleChooseImage} />
    </View>
  );
};

export default OtherScreen;
